<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\storage;

#use tehframework\testing\phpunit\TestCase;

require_once 'test/testHelper.php';

/**
 * @todo
 */
class ApcStorageTest extends TestCase
{
	/**
	 * @var ApcStorage
	 */
	private $storage;
	
	protected function beforeTest()
	{
		if (!function_exists('apc_fetch'))
		{
			$this->markTestSkipped('APC module not available.');
			
			return;
		}
		
		apc_clear_cache('user');
		
		$this->storage = new ApcStorage();
	}
	
	function testStoreThrowsExceptionIfIdIsNull()
	{
		$this->setExpectedException('InvalidArgumentException');
		
		$this->storage->store(null, 1);
	}
	
	function testStoreThrowsExceptionIfIdIsEmpty()
	{
		$this->setExpectedException('InvalidArgumentException');
		
		$this->storage->store('', 1);
	}
	
	function testStoreThrowsExceptionIfDataIsNull()
	{
		$this->setExpectedException('InvalidArgumentException');
		
		$this->storage->store('foo', null);
	}
	
	function testStoreAddsGivenDataUnderSpecifiedId()
	{
		$this->storage->store('foo', 'bar')->store('bar', 'foo');
		
		$this->assertSame(array('foo' => 'bar', 'bar' => 'foo'),
			$this->storage->getDataMap());
	}
	
	function testRestoreReturnsDataStoredUnderSpecifiedId()
	{
		$this->storage->store('foo', 'bar');
		
		$this->assertSame('bar', $this->storage->restore('foo'));
	}
	
	function testRestoreReturnsNullIfNoDataStoredUnderSpecifiedId()
	{
		$this->assertNull($this->storage->restore('hopefullyIDoNotExist'));
	}
	
	function testDeleteRemovesDataStoredUnderSpecifiedId()
	{
		$this->assertNull(
			$this->storage->store('foo', 1)->delete('foo')->restore('foo'));
	}
	
	function testDeleteDoesNothingIfNoDataExistsUnderSpecifiedId()
	{
		$this->assertSame($this->storage,
			$this->storage->delete('hopefullyIDoNotExist'));
	}
	
	function testClearRemovesAllStoredData()
	{
		$this->assertSame(array(), $this->storage->clear()->getDataMap());
		$this->assertSame(array(),
			$this->storage->store('foo', 1)->store('bar', 2)->clear()->getDataMap());
	}
}